package com.kitchen
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.NetStatusEvent;
	import flash.net.NetConnection;
	import flash.net.Responder;
	
	public class AbstractProxy implements IEventDispatcher
	{
		public static var E_SERROR:String = 'netorsererror';
		public static var E_CERROR:String = 'clienterror';
		public static var E_OK:String = 'ok';
		
		public var name:String;
		public var result:Object;
		protected var gateWay:String;
		protected var connect:NetConnection = null;
		protected var dispatcher:EventDispatcher;
		
		public function AbstractProxy()
		{
			this.dispatcher = new EventDispatcher(this);
			this.gateWay = 'http://www.meichu.com/gateway.php';
		}
		
		protected function getConnect():NetConnection{
			if(this.connect == null){
				this.connect = new NetConnection();
				this.connect.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandle);
				this.connect.connect(this.gateWay);
			}
			return this.connect;
		}
		
		protected function netStatusHandle(nse:NetStatusEvent):void{
			trace('proxy.netStatusHandle');
			var info:Object = nse.info;
			switch(info.code){
				case 'NetConnection.Connect.Rejected':
				case 'NetConnection.Connect.Failed':
				case 'NetConnection.Connect.Closed':
				case 'NetConnection.Call.BadVersion':
				this.result = info.code;
				this.dispatchEvent(new Event(E_SERROR));
				break;
				case 'NetConnection.Connect.Success':
				default:
				break;
			}
		}
		
		protected function resultHandle(results:Object):void{
			trace('AbstractProxy.proxy.resultHandle');
			this.result = results;
			this.dispatchEvent(new Event(E_OK));
		}
		
		protected function faultHandle(fault:Object):void{
			trace('AbstractProxy.proxy.faultHandle');
			this.result = fault.description;
			this.dispatchEvent(new Event(E_CERROR));
		}

		public function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void
		{
			this.dispatcher.addEventListener(type, listener, useCapture, priority);
		}
		
		public function removeEventListener(type:String, listener:Function, useCapture:Boolean=false):void
		{
			this.dispatcher.removeEventListener(type, listener, useCapture);
		}
		
		public function dispatchEvent(event:Event):Boolean
		{
			return this.dispatcher.dispatchEvent(event);
		}
		
		public function hasEventListener(type:String):Boolean
		{
			return this.dispatcher.hasEventListener(type);
		}
		
		public function willTrigger(type:String):Boolean
		{
			return this.dispatcher.willTrigger(type);
		}

	}
}